import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/visitor/record_detail_controller.dart';
import 'package:qr_flutter/qr_flutter.dart';

import '../../../../models/home_visitor_record_detail_entity.dart';
import '../../../../widget/custom_app_bar.dart';

class VisitorRecordDetail extends StatelessWidget {
  final int visitorId;

  const VisitorRecordDetail({
    super.key,
    required this.visitorId,
  });

  @override
  Widget build(BuildContext context) {
    var appBar = CustomAppBar("", backgroundColor: Colors.transparent);
    var heightOfAppBar = appBar.preferredSize.height;
    return GetBuilder(
      init: RecordDetailController(visitorId),
      builder: (controller) => Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
                image: DecorationImage(
                    image: AssetImage("assets/images/page_bg.png"),
                    fit: BoxFit.fill)),
            child: Scaffold(
              backgroundColor: Colors.transparent,
              appBar: appBar,
              body: Obx(
                () => controller.hadLoadFinished.value
                    ? GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () =>
                            FocusManager.instance.primaryFocus?.unfocus(),
                        child: Center(
                          child: _builderBodyWidget(controller),
                        ),
                      )
                    : Container(),
              ),
            ),
          ),
          Positioned(
              top: heightOfAppBar + 72.h,
              left: 56.w,
              child: Text(
                "访客通行证",
                style: TextStyle(fontSize: 40.sp, fontWeight: FontWeight.w500),
              )),
          Positioned(
            top: 78.h,
            right: 0.w,
            child: Image.asset(
              "assets/images/home_visitor_record_icon.png",
              width: 280.w,
              height: 250.h,
            ),
          ),
        ],
      ),
    );
  }

  Widget _builderBodyWidget(RecordDetailController controller) {
    return Column(
      children: [
        SizedBox(
          height: 94.h,
        ),
        Container(
            width: 686.w,
            height: 1300.h,
            alignment: Alignment.center,
            padding: EdgeInsets.only(top: 52.h),
            decoration: const BoxDecoration(
              image: DecorationImage(
                  image: AssetImage("assets/images/home_visitor_record_bg.png"),
                  fit: BoxFit.fill),
            ),
            child: Column(
              children: [
                Text(
                  "请将此页面出示给门岗人员",
                  style:
                      TextStyle(fontSize: 32.sp, fontWeight: FontWeight.w500),
                ),
                SizedBox(
                  height: 20.h,
                ),
                _buildQRWidget(controller.info),
                SizedBox(
                  height: 40.h,
                ),
                _buildFormWidget(controller.info)
              ],
            ))
      ],
    );
  }

  Widget _buildQRWidget(HomeVisitorRecordDetailObjInfo info) {
    var startTime = info.visitStartTime
        .substring(0, info.visitStartTime.lastIndexOf("+") - 1);
    var endTime =
        info.visitEndTime.substring(0, info.visitEndTime.lastIndexOf("+") - 1);
    var qrData =
        "${info.qrCodeUrl}?room=${info.address}&visitor=${info.visitor}&phoneNumber=${info.phoneNumber}&visitorNum=${info.visitorNum}&visitStartTime=$startTime&visitEndTime=$endTime&visitReason=${info.visitReason}";
    return Container(
      width: 480.h,
      height: 480.h,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/images/home_visitor_qr_bg.png"),
          fit: BoxFit.fill,
        ),
      ),
      child: QrImage(
        padding: EdgeInsets.all(20.w),
        data: qrData,
        version: QrVersions.auto,
      ),
    );
  }

  Widget _buildFormWidget(HomeVisitorRecordDetailObjInfo info) {
    return Padding(
      padding: EdgeInsets.only(left: 88.w),
      child: SizedBox(
        height: 550.h,
        child: Stack(
          children: [
            Positioned(
              child: _buildSingleRow("访客姓名", info.visitor),
            ),
            Positioned(
              top: 56.h,
              child: _buildSingleRow("访客电话", info.phoneNumber),
            ),
            Positioned(
              top: 112.h,
              child: _buildSingleRow("访客人数", info.visitorNum.toString()),
            ),
            Positioned(
              top: 168.h,
              child: _buildSingleRow("到访地址", info.address),
            ),
            Positioned(
              top: 224.h,
              child: _buildSingleRow(
                "拜访时段",
                "${info.visitStartTime.substring(0, info.visitStartTime.lastIndexOf("+"))}~",
                text2: info.visitEndTime
                    .substring(0, info.visitEndTime.lastIndexOf("+")),
              ),
            ),
            Positioned(
              top: 312.h,
              child: Divider(
                thickness: 2.h,
                color: const Color(0XFFF0F2F7),
              ),
            ),
            Positioned(
              top: 328.h,
              child: _buildLabelText("来访目的"),
            ),
            Positioned(
              top: 372.h,
              child: SizedBox(
                width: 510.w,
                child: _buildText(info.visitReason),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSingleRow(String labelText, String text, {String text2 = ""}) {
    return Padding(
      padding: EdgeInsets.only(top: 16.h),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildLabelText(labelText),
          SizedBox(
            width: 42.w,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildText(text),
              text2.isNotEmpty ? _buildText(text2) : Container(),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildLabelText(String content) {
    return Text(
      content,
      style: TextStyle(fontSize: 32.sp, color: const Color(0xFF7C7E80)),
    );
  }

  Widget _buildText(String content) {
    return Text(
      content,
      style: TextStyle(fontSize: 32.sp, color: const Color(0xFF434649)),
    );
  }
}
